package org.redis.cluster.transport.netty.test;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger;

import org.redis.cluster.config.ClientConfig;
import org.redis.cluster.redis.transport.codec.client.RedisClientProtocolDecoder;
import org.redis.cluster.redis.transport.codec.client.RedisClientProtocolEncoder;
import org.redis.cluster.transport.netty.client.NettyClient;

public class NettyClientTester_3 {
	
	public static void main(String[] args) throws Exception {
		for(int k=0;k<100;k++) {
			CyclicBarrier cyclicBarrier=new CyclicBarrier(11);
			AtomicInteger count=new AtomicInteger();
			AtomicInteger sum=new AtomicInteger();
			
			for(int i=0;i<10;i++) {
				new Thread(()->{
					ClientConfig clientConfig=new ClientConfig();
					clientConfig.setHost("127.0.0.1");
					clientConfig.setPort(6379);
					clientConfig.setTimeout(1000*3);
					clientConfig.setConnection(100);
					NettyClient client=new NettyClient(clientConfig, new RedisClientProtocolDecoder(), new RedisClientProtocolEncoder());
					
					long start=System.currentTimeMillis();
					
					for(int j=0;j<10000;j++) {
						try {
							byte response[]=client.send("*1\r\n$4\r\nPING\r\n".getBytes());
						} catch (Exception e) {
							e.printStackTrace();
						}
						count.incrementAndGet();
					}
					
					long end=System.currentTimeMillis();
					sum.addAndGet((int) (end-start));
					System.out.println(end-start);
					client.close();
					try {
						cyclicBarrier.await();
					}
					catch(Exception e) {
						e.printStackTrace();
					}
				}).start();
			}
			
			cyclicBarrier.await();
			System.out.println("avg:"+sum.intValue()/(float) count.intValue());
		}
	}
}